package com.iotechn.unimall.admin.api.biaozhun;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.gson.Gson;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.AdminDO;
import com.iotechn.unimall.data.domain.ShopCailiaoPiciDO;
import com.iotechn.unimall.data.dto.AdminDTO;
import com.iotechn.unimall.data.mapper.ShopCailiaoMapper;
import com.iotechn.unimall.data.mapper.ShopCailiaoPiciMapper;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminCailiaoPiciServiceImpl implements AdminCailiaoPiciService {
    @Autowired
    private ShopCailiaoPiciMapper shopCailiaoMapper;

 

    @Override
    public Page<ShopCailiaoPiciDO> list(Long  shopId,String title,Integer page, Integer limit,  Long adminId) throws ServiceException {
        Wrapper<ShopCailiaoPiciDO> wrapper = new EntityWrapper<ShopCailiaoPiciDO>();
        if(shopId!=null){
            wrapper.eq("shop_id",shopId);
        }

        if(!StringUtils.isEmpty(title)){
            wrapper.like("pici_name",title);
        }
        wrapper.orderBy("id", false);
        Integer count = shopCailiaoMapper.selectCount(wrapper);
        List<ShopCailiaoPiciDO> ShopCailiaoPiciDOS = shopCailiaoMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(ShopCailiaoPiciDOS, page, limit, count);
    }
    @Override
    public List<ShopCailiaoPiciDO> getBiaozhunByShopId(Long shopId) throws ServiceException {
         List<ShopCailiaoPiciDO> ShopCailiaoPiciDOS = shopCailiaoMapper.getBiaozhunByShopId(shopId);
         return ShopCailiaoPiciDOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShopCailiaoPiciDO create(ShopCailiaoPiciDO ShopCailiaoPiciDO, Long adminId) throws ServiceException {
        ShopCailiaoPiciDO.setAdminId(adminId);
        AdminDTO adminDO= SessionUtil.getAdmin();
        Date now = new Date();
        ShopCailiaoPiciDO.setGmtCreate(now);
        ShopCailiaoPiciDO.setGmtUpdate(now);
        ShopCailiaoPiciDO.setLuruId(adminId+"");
        if (shopCailiaoMapper.insert(ShopCailiaoPiciDO) > 0) {
            ShopCailiaoPiciDO.setId(ShopCailiaoPiciDO.getId());
            return ShopCailiaoPiciDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(ShopCailiaoPiciDO ShopCailiaoPiciDO, Long adminId) throws ServiceException {
        Long id = ShopCailiaoPiciDO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();

        ShopCailiaoPiciDO.setGmtUpdate(now);
        if (shopCailiaoMapper.updateById(ShopCailiaoPiciDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (shopCailiaoMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids)) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        for (Long spuId : ids) {
            shopCailiaoMapper.deleteById(spuId);
        }
        return "ok";
    }


    @Override
    public ShopCailiaoPiciDO detail(Long id, Long adminId) throws ServiceException {
        return shopCailiaoMapper.selectById(id);
    }

    @Override
    public Boolean updateStatus(Long adminId, Long id, Integer status) throws ServiceException {
        if (status == null || (status != 0 && status != 1)) {
            throw new AdminServiceException(ExceptionDefinition.USER_INFORMATION_MISSING);
        }
        ShopCailiaoPiciDO ShopCailiaoPiciDO = new ShopCailiaoPiciDO();
        ShopCailiaoPiciDO.setId(id);
        ShopCailiaoPiciDO.setGmtUpdate(new Date());
        if (shopCailiaoMapper.updateById(ShopCailiaoPiciDO) > 0) {
            return true;
        }
        return false;
    }

}